package com.jwgf.smarowindingmachine_unity.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jwgf.adapter.MultiItemTypeAdapter;
import com.jwgf.group.Group;
import com.jwgf.itemviewdelegates.CraftSelectAttr;
import com.jwgf.itemviewdelegates.CraftSelectDelegate;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.SpindleParameter;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;
import com.jwgf.smarowindingmachine_unity.logic.SmaroWindingMachineDataAccessRules;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class GroupEditDialog extends DialogFragment {
    public static final String GROUP = "group";
    private AlertDialog dlg;
    private EditText start;
    private EditText end;

    private RecyclerView craftRecycler;
    private MultiItemTypeAdapter<Object> craftAdapter;

    private long selectedId;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Bundle bd = getArguments();
        Group group = (Group) bd.getSerializable(GROUP);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.group_edit_view, null);
        start = v.findViewById(R.id.start_value);
        end = v.findViewById(R.id.end_value);
        start.setText(String.valueOf(group.getStartSpindle()));
        end.setText(String.valueOf(group.getEndSpindle()));
        selectedId = group.getId();

        final int max = Integer.valueOf(MachineParameterSharedPreference.getInstance().getValue(0x29, 1));
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String strStart = start.getText().toString();
                String strEnd = end.getText().toString();
                int start = Integer.valueOf(strStart);
                int end = Integer.valueOf(strEnd);
                String key = String.valueOf(0x29) + "-" + 1;
                boolean isValidStart = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strStart);
                boolean isValidEnd = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strEnd);
                if (isValidStart && isValidEnd && start <= max && end <= max && start <= end) {
                    EventBus.getDefault().post(new Group(start, end, selectedId));
                } else {
                    Toast.makeText(getActivity(), "单锭范围超限", Toast.LENGTH_LONG).show();
                }
            }
        });

        List<SpindleParameter> parameters = ObjectBox.get().boxFor(SpindleParameter.class).getAll();
        final List list = new ArrayList();
        for (SpindleParameter para : parameters) {
            CraftSelectAttr attr = new CraftSelectAttr();
            attr.setCraftName(para.craftName);
            attr.setId(para.id);
            if (para.id == selectedId) {
                attr.setSelected(true);
            }
            list.add(attr);
        }

        craftRecycler = v.findViewById(R.id.craft_recycler);
        craftAdapter = new MultiItemTypeAdapter<>(getActivity(), list);
        craftAdapter.addItemViewDelegate(new CraftSelectDelegate());
        craftAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (position == -1)
                    return;
                for (Object o : list) {
                    CraftSelectAttr attr = (CraftSelectAttr) o;
                    attr.setSelected(false);
                }
                ((CraftSelectAttr) list.get(position)).setSelected(true);
                selectedId = ((CraftSelectAttr) list.get(position)).getId();
                craftAdapter.notifyDataSetChanged();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        GridLayoutManager craftLayoutManager = new GridLayoutManager(getActivity(), 1);
        craftRecycler.setLayoutManager(craftLayoutManager);
        craftRecycler.setAdapter(craftAdapter);


        String title = getString(R.string.group) + getString(R.string.edit);
        builder.setTitle(title);
        builder.setView(v);
        dlg = builder.create();
        return dlg;
    }

}
